<template>
  <!--编写页面静态部分，即view部分-->
  <div>
    <el-form :model="templateForm" label-width="80px" :rules="templateFormRules" ref="templateForm">
      <el-form-item label="所属站点" prop="siteId">
        <el-select v-model="templateForm.siteId" placeholder="请选择站点">
          <el-option
            v-for="item in siteList"
            :key="item.siteId"
            :label="item.siteName"
            :value="item.siteId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input v-model="templateForm.templateName" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="模板参数" prop="templateParameter">
        <el-input v-model="templateForm.templateParameter" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="模板文件ID" prop="templateFileId">
        <el-input v-model="templateForm.templateFileId" auto-complete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addSubmit">提交</el-button>
      <el-button  @click="go_back" >返回</el-button>
    </div>
  </div>
</template>
<script>
  import * as cmsApi from '../api/cms'

  /*编写页面静态部分，即model及vm部分。*/
  export default {
    data() {
      return {
        //站点列表
        siteList: [],
        //新增界面数据
        templateForm: {
          siteId: '',
          templateName: '',
          templateParameter: '',
          templateFileId: '',
        },
        templateFormRules: {
          siteId:[
            {required: true, message: '请选择站点', trigger: 'blur'}
          ],
          templateName:[
            {required: true, message: '请输入模版名称', trigger: 'blur'}
          ],
          templateParameter: [
            {required: false, message: '请输入模板参数', trigger: 'blur'}
          ],
          templateFileId: [
            {required: true, message: '请输入模板文件Id', trigger: 'blur'}
          ],
        },
      }
    },
    methods: {
      //查询站点下拉菜单列表数据
      querySite: function(){
        cmsApi.select_site_list().then((res)=>{
          this.siteList = res.queryResult.list;
        })
      },
      addSubmit: function(){
        this.$refs['templateForm'].validate((valid) => {
          if (valid) {
            //表单校验成功
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //调用page_add方法请求服务端新增接口
              cmsApi.template_add(this.templateForm).then((res) => {
                  if(res.success){
                   /* this.$message({
                      message: '提交成功',
                      type: 'success'
                    });*/
                    this.$message.success('提交成功');
                    //提交成功清空表单数据
                    this.$refs['templateForm'].resetFields();
                    //返回列表页面
                    this.go_back();
                  }else if(res.message){
                    this.$message.error(res.message);
                  }else{
                    this.$message.error('提交失败');
                  }
                });
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      go_back(){
        this.$router.push({
          path: '/cms/template/list', query: {
            page: this.$route.query.page,
            siteId: this.$route.query.siteId//取出当前路由中的参数
          }
        })
      }
    },
    //调用钩子方法
    mounted() {
      this.querySite();
    }
  }
</script>
<style>
  /*编写页面样式，不是必须*/
</style>
